package com.jingdianjichi.subject.domain.service.Impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.IsDeletedCodeEnum;
import com.jingdianjichi.subject.domain.convent.SubjectCategoryConventer;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBo;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBo subjectCategoryBo) {

        SubjectCategory subjectCategory = SubjectCategoryConventer.INSERT.conventBoToCategory(subjectCategoryBo);

        subjectCategory.setIsDeleted(IsDeletedCodeEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);


    }


    @Override
    public List<SubjectCategoryBo> queryCategoryByPrimary(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = new SubjectCategory();

        BeanUtils.copyProperties(subjectCategoryBo, subjectCategory);
        subjectCategory.setIsDeleted(IsDeletedCodeEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryBo> boList = SubjectCategoryConventer.INSERT.conventBoToCategory(subjectCategoryList);

        if (log.isInfoEnabled()) {
            log.info("subjectCategoryService.queryPrimaryCategory.boList:{}", JSON.toJSONString(boList));
        }

        return boList;
    }

    @Override
    public Boolean update(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = new SubjectCategory();
        BeanUtils.copyProperties(subjectCategoryBo, subjectCategory);

        int update = subjectCategoryService.update(subjectCategory);
        return update>0;

    }

    @Override
    public Boolean delect(SubjectCategoryBo subjectCategoryBo) {

        SubjectCategory subjectCategory = new SubjectCategory();
        BeanUtils.copyProperties(subjectCategoryBo, subjectCategory);
        subjectCategory.setIsDeleted(IsDeletedCodeEnum.DELETED.getCode());
        int update = subjectCategoryService.update(subjectCategory);
        return update>0;
    }


}
